package my.zyf.monitor;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

public class CustomHttpClient {

	private CloseableHttpClient client = HttpClients.createDefault();

	public <T> T post(String url, List<NameValuePair> pair, Class<T> clazz)
			throws ClientProtocolException, IOException {
		HttpPost httpPost = new HttpPost(url);
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pair,
				"UTF-8");

		httpPost.setEntity(formEntity);

		HttpResponse response = client.execute(httpPost);

		String result = EntityUtils.toString(response.getEntity());
		
		return JSONObject.parseObject(result, clazz);
	}

}
